import Vue from 'vue'
import Router from 'vue-router'
import product from './modules/product'
import order from './modules/order'
import user from './modules/user'
import operation from './modules/operation'
import agent from './modules/agent'
import financial from './modules/financial'
import other from './modules/other'
import activity from './modules/activity'
import company from './modules/company'
import users from './modules/users'
import interaction from './modules/interaction'
import system from './modules/system'
import SettingEnv from '@/setting.env'

Vue.use(Router)
/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: `${SettingEnv.routerMode === 'history' ? SettingEnv.customRouter : '/'}`,
    component: Layout,
    redirect: `${SettingEnv.customRouter}/index`,
    children: [
      {
        path: `${SettingEnv.customRouter}/index`,
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '主页', icon: 'el-icon-s-home', affix: true }
      }
    ]
  },
  {
    path: `${SettingEnv.customRouter}/redirect`,
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)?',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  system,
  activity,
  company,
  users,
  interaction,
  product,
  order,
  user,
  financial,
  agent,
  other,
  operation,
  {
    path: `${SettingEnv.customRouter}/login`,
    name: 'login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: `${SettingEnv.customRouter}/login/:id`, // 使用动态路由参数
    name: 'loginWithId', // 命名这个路由以便在重定向时使用
    redirect: to => {
      const { hash } = window.location; // 获取当前页面的 hash 值
      return {
        name: 'login',
        params: { id: to.params.id }, // 将原始的动态参数 id 传递给 login 页面
        hash: hash.substring(1) // 将原始的 hash 值传递给 login 页面
      };
    }
  },
  {
    path: `${SettingEnv.customRouter}/setting/uploadFrom`,
    component: () => ('@/components/uploadFrom/index'),
    name: 'uploadFrom'
  },
  {
    path: `${SettingEnv.customRouter}/setting/video`,
    component: () => ('@/components/iconFrom/index'),
    name: 'videoFrom'
  },
  {
    path: `${SettingEnv.customRouter}/setting/uploadPicture`,
    component: () => import('@/components/uploadPicture/index.vue'),
    name: 'uploadPicture'
  },
  {
    path: `${SettingEnv.customRouter}/setting/uploadVideo`,
    component: () => import('@/components/uploadVideo/index.vue'),
    name: 'uploadPicture'
  },
  {
    path: `${SettingEnv.customRouter}/404`,
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: `${SettingEnv.customRouter}/*`,
    redirect: SettingEnv.customRouter + '/404',
    hidden: true
  }
]

const createRouter = () => new Router({
  mode: SettingEnv.routerMode, // 需要服务支持
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// 路由器重置
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
